<?php

class ChannelController extends Controller {

    public $sort;
    public $order;
    public $page;
    public $pageNum;
    public $pageView;
    public $cookie;
    public $zonecookie;
    public $zoneid;

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow authenticated users to access all actions
                'users' => array('*'),
            ),
        );
    }

    public function init() {
        $this->sort = $_GET['sort'];
        $this->order = $_GET['order'];
        $this->page = !$_GET['page'] ? 1 : $_GET['page'];
        $this->pageNum = 16;
        $this->pageView = ($this->page - 1) * $this->pageNum;
        
        /*
         * 获取cookie商圈ID
         */
        $this->cookie = Yii::app()->request->getCookies();
        $this->zonecookie = !$this->cookie['zone'] ? '2' : $this->cookie['zone'];
        $this->zoneid = !$this->zonecookie ? '&fq=zoneId:2' : '&fq=zoneId:'.$this->zonecookie;
        
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/common.css');
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/v3/n_common.css');
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/food.css');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/common.js", CClientScript::POS_HEAD);
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/v3/n_common.js", CClientScript::POS_HEAD);
        $cs->registerCoreScript('jquery');
        $cs->registerCoreScript('jquery.ui');
    }

    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionSnack() {
        $this->pageTitle = '美味小吃';
        if ($this->sort == '' && $this->order == '') {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:1'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum.'&sort=createTime+asc');
        } else {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:1'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum.'&sort=' . $this->sort . '+' . $this->order);
        }
        $model = CJSON::decode($row);

        /*
         * 分页
         */
        $numFound = $model['numFound'];
        $pager = new CPagination($numFound);
        $pager->pageSize = 16;

        $this->render('snack', array('model' => $model['QueryResponse']['results'], 'numFound' => $numFound, 'pages' => $pager));
    }

    public function actionTake() {
        $this->pageTitle = '白领组合';
        if ($this->sort == '' && $this->order == '') {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:2'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum.'&sort=createTime+asc');
        } else {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:2'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum.'&sort=' . $this->sort . '+' . $this->order);
        }
        $model = CJSON::decode($row);

        /*
         * 分页
         */
        $numFound = $model['numFound'];
        $pager = new CPagination($numFound);
        $pager->pageSize = 16;

        $this->render('take', array('model' => $model['QueryResponse']['results'],'numFound' => $numFound, 'pages' => $pager));
    }

    public function actionFruit() {
        $this->pageTitle = '沙拉果园';
        if ($this->sort == '' && $this->order == '') {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:4'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum);
        } else {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:4'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum.'&sort=' . $this->sort . '+' . $this->order);
        }
        $model = CJSON::decode($row);

        /*
         * 分页
         */
        $numFound = $model['numFound'];
        $pager = new CPagination($numFound);
        $pager->pageSize = 16;

        $this->render('fruit', array('model' => $model['QueryResponse']['results'],'numFound' => $numFound, 'pages' => $pager));
    }
    
    public function actionDrink() {
        $this->pageTitle = '缤纷酷饮';
        if ($this->sort == '' && $this->order == '') {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:5'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum);
        } else {
            $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=productTypeId:5'.$this->zoneid.'&fq=status:1&wt=json&indent=true&start='.$this->pageView.'&rows='.$this->pageNum.'&sort=' . $this->sort . '+' . $this->order);
        }
        $model = CJSON::decode($row);

        /*
         * 分页
         */
        $numFound = $model['numFound'];
        $pager = new CPagination($numFound);
        $pager->pageSize = 16;

        $this->render('drink', array('model' => $model['QueryResponse']['results'],'numFound' => $numFound, 'pages' => $pager));
    }

}

?>